package com.example.demo.controller;
/*
 * 李洲 2020030654A1213
 * 2024.05.24
 */
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    // 处理没有请求参数的GET请求
    @GetMapping
    public String getDefaultUser() {
        return "Fetching default user";
    }

    // 处理带有一个请求参数的GET请求
    @GetMapping("/byname")
    public String getUserByName(@RequestParam String name) {
        return "Fetching user with name: " + name;
    }

    // 处理带有一个请求参数并带有默认值的GET请求
    @GetMapping("/bynamewithdefault")
    public String getUserByNameWithDefault(@RequestParam(defaultValue = "John Doe") String name) {
        return "Fetching user with name: " + name;
    }

    // 处理带有多个请求参数的GET请求
    @GetMapping("/bycriteria")
    public String getUserByCriteria(@RequestParam String name, @RequestParam int age) {
        return "Fetching user with name: " + name + " and age: " + age;
    }

    // 处理可选的请求参数的GET请求
    @GetMapping("/optional")
    public String getUserOptional(@RequestParam(required = false) String name) {
        if (name == null) {
            return "Fetching user with no specific name";
        }
        return "Fetching user with name: " + name;
    }

    // 处理带有POST请求参数的请求
    @PostMapping("/create")
    public String createUser(@RequestParam String name, @RequestParam int age) {
        return "Creating user with name: " + name + " and age: " + age;
    }
}
